package com.ncloud.billing.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.billing.entity.BsCycle;
import com.ncloud.billing.entity.ChnlAdjustBill;
import com.ncloud.billing.entity.ChnlBill;
import com.ncloud.billing.entity.ChnlDetailBill;
import com.ncloud.billing.mapper.BsCycleMapper;
import com.ncloud.billing.mapper.ChnlAdjustBillMapper;
import com.ncloud.billing.mapper.ChnlBillMapper;
import com.ncloud.billing.mapper.ChnlDetailBillMapper;
import com.ncloud.billing.mapper.ChnlMapper;
import com.ncloud.billing.mapper.ChnlRealBillMapper;
import com.ncloud.billing.service.GenChnlBillService;
import com.ncloud.param.service.SequenceService;

@Service
public class GenChnlBillServiceImpl implements GenChnlBillService{
	private static final Logger logger = LogManager.getLogger(GenChnlBillServiceImpl.class);
	@Resource
	private BsCycleMapper bsCycleMapper;
	@Resource
	private ChnlRealBillMapper chnlRealBillMapper;
	@Resource
	private ChnlBillMapper chnlBillMapper;
	@Resource
	private ChnlDetailBillMapper chnlDetailBillMapper;
	@Resource
	private ChnlMapper chnlMapper;
	@Resource
	private ChnlAdjustBillMapper chnlAdjustBillMapper;
	@Reference
	private SequenceService sequenceService;
	@Override
	public JSONObject genChnlBill(JSONObject json) throws ParseException {
		//当前账期
		List<BsCycle> currentCycle = bsCycleMapper.getCurrentCycle();
		if (currentCycle.size()==0){return json;}
		Integer current_cycle_id=currentCycle.get(0).getCycle_id();
		//未出账账单账期
		List<BsCycle> minBillCycle = bsCycleMapper.getMinBillCycle();
		if (minBillCycle.size()==0){
			logger.info("该账期已出账============================");
			return json;
		}
		Integer bill_cycle_id=minBillCycle.get(0).getCycle_id();
		/*if(json.containsKey("bill_cycle_id")){
			bill_cycle_id = json.getInteger("bill_cycle_id");
		}*/
		//=====================================================================
		//所有一级渠道
		logger.info("结算单生成开始============================");
		List<JSONObject> chnls = chnlMapper.getChnlByChnlLevel("1");
		for(int chnlIndex=0;chnlIndex<chnls.size();chnlIndex++){
			JSONObject chnl = chnls.get(chnlIndex);
			Long chnl_id = chnl.getLong("chnl_id");
			ChnlBill chnlBill=new ChnlBill();
			Long total_id=sequenceService.getSequence("total_id");
			Long fee=0l;
			Long balance=0l;
			List<ChnlDetailBill> chnlDetailBills= chnlRealBillMapper.createChnlDetailBillByCycleID(bill_cycle_id,chnl_id);
			for(int chnlDetailBillIndex=0;chnlDetailBillIndex<chnlDetailBills.size();chnlDetailBillIndex++){
				ChnlDetailBill chnlDetailBill=chnlDetailBills.get(chnlDetailBillIndex);
				chnlDetailBill.setTotal_id(total_id);
				chnlDetailBill.setParent_id(chnl_id);
				chnlDetailBill.setUpdate_time(new Date());
				//如果费用为0 直接标记为已经结清
				if(chnlDetailBill.getFee()==0){
					chnlDetailBill.setBill_tag("2");
				}else{
					chnlDetailBill.setBill_tag("1");
				}
				chnlDetailBillMapper.insertChnlDetailBill(chnlDetailBill);
				chnlRealBillMapper.updateChnlRealBillTag(0l,bill_cycle_id,chnlDetailBill.getChnl_id());
				fee+=chnlDetailBill.getFee();
				balance+=chnlDetailBill.getBalance();
			}
			chnlBill.setTotal_id(total_id);
			chnlBill.setChnl_id(chnl_id);
			chnlBill.setChnl_name(chnl.getString("chnl_name"));
			chnlBill.setCycle_id(bill_cycle_id);
			chnlBill.setFee(fee);
			chnlBill.setBalance(balance);
			if(fee==0){
				chnlBill.setBill_tag("2");
			}else{
				chnlBill.setBill_tag("1");
			}
			chnlBill.setCreate_time(new Date());
			chnlBill.setCreate_user_id(0l);
			chnlBill.setUpdate_time(new Date());
			chnlBill.setUpdate_user_id(0l);
			chnlBill.setRemark("");
			//修改调账记录标记
			ChnlAdjustBill chnlAdjustBill = new ChnlAdjustBill();
			chnlAdjustBill.setChnl_id(chnl_id);
			chnlAdjustBill.setAdjust_cycle_id(bill_cycle_id);
			chnlAdjustBill.setPay_tag("1");//0-初始状态 1-未冲正 2-已冲正 3-部分冲正
			chnlAdjustBill.setUpdate_time(new Date());
			chnlAdjustBill.setUpdate_user_id(0l);
			//chnlAdjustBillMapper.insertChnlAdjustBill(chnlAdjustBill);
			chnlBillMapper.insertChnlBill(chnlBill);
			chnlAdjustBillMapper.updateChnlAdjustBill(chnlAdjustBill);
			logger.info("结算单生成  账期:"+bill_cycle_id+ " 渠道:"+chnl_id+" 账单数量:"+chnlDetailBills.size());
		}
		BsCycle billCycle = new BsCycle();
		billCycle.setCycle_id(bill_cycle_id);
		billCycle.setAct_tag("1");
		BsCycle curCycle = new BsCycle();
		curCycle.setCycle_id(current_cycle_id);
		curCycle.setUse_tag("1");
		bsCycleMapper.updateCycleActTag(billCycle);
		bsCycleMapper.updateCycleUseTag(curCycle);
		logger.info("结算单生成结束============================");
		return json;
	}
	@Transactional(propagation=Propagation.REQUIRED)
	private void moveRealBill(ChnlBill chnlBill) {
		chnlBillMapper.insertChnlBill(chnlBill);
		//chnlRealBillMapper.deleteChnlRealBillByBillId(chnlBill.getBill_id());
	}
}
